package com.xur.springboot.service.impl;

import com.xur.springboot.entity.User;
import com.xur.springboot.mapper.UserMapper;
import com.xur.springboot.service.UserService;
import com.xur.springboot.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //查询所有用户
    @Override
    public List<User> queryUser() {
        return this.userMapper.queryUser();
    }

    @Override
    public ResultVO login(String username, String password, int role) {
        // 检查用户名是否为空
        if (!StringUtils.hasText(username)) {
            return new ResultVO(1000, "用户名不能为空", null);
        }

        // 查询用户
        User user = userMapper.login(username);
        if (user == null) {
            return new ResultVO(1000, "用户名不存在", null);
        }
        System.out.println("用户ID: " + user.getId()); // 从数据库获取ID
        /*// 验证密码
        if (password.equals(user.getPassword()) && role == user.getRole()) {
            return new ResultVO(1001, "登陆成功", user);
        } else if(){
            return new ResultVO(1002, "密码错误或身份不正确", null);
        }else{
            // 两者都错误
            return new ResultVO(1004, "密码错误且身份不正确", null);
        }*/

        // 检查账号状态 (1=正常，0=禁用)
        if (user.getStatus() != null && user.getStatus() == 0) {
            return new ResultVO(1005, "账号已被禁用", null);
        }
        // 验证密码和身份分开判断
        if (password.equals(user.getPassword())) {
            if (role == user.getRole()) {
                System.out.println(user);
                return new ResultVO(1001, "登陆成功", user);
            } else {
                return new ResultVO(1002, "身份不正确", null);
            }
        } else {
            if (role == user.getRole()) {
                return new ResultVO(1003, "密码错误", null);
            } else {
                return new ResultVO(1004, "密码和身份均不正确", null);
            }
        }

    }

    @Override
    public int queryIdByUsername(String username, String password) {
        return this.userMapper.queryIdByUsername(username,password);
    }

    @Override
    public List<User> queryUserById(int id) {
        return this.userMapper.queryUserById(id);
    }


    @Override
    public int addUserInfo(User user) {
        return this.userMapper.addUserInfo(user);
    }

    // UserServiceImpl.java 实现
    @Override
    public int updateUserStatus(Integer userId, Integer status) {
        // 参数校验
        if (userId == null || status == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        if (status < 0 || status > 1) {
            throw new IllegalArgumentException("非法状态值");
        }
        return userMapper.updateUserStatus(userId, status);
    }

    @Override
    public List<User> queryUsersByDynamicFields(List<String> fields, Integer targetId) {
        return userMapper.queryUsersByDynamicFields(fields, targetId);
    }

    @Override
    public List<User> queryUsersByIds(List<Integer> ids) {
        return userMapper.queryUsersByIds(ids);
    }

}